package com.wu.foodsafetyplayer.widget;

import android.content.Context;
import android.os.Handler;
import android.os.Message;
import android.util.AttributeSet;
import android.util.Log;
import android.widget.TextView;

import com.wu.foodsafetyplayer.http.Func;
import com.wu.foodsafetyplayer.utils.tv.TimeUtils;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import java.io.IOException;

/**
 * Like AnalogClock, but digital. Shows seconds.
 */
public class WeatherTextView2 extends TextView {
	private String TAG = WeatherTextView2.class.getSimpleName();
	private String wfUrl = "https://tianqi.moji.com/weather/china/";
	private String nongliUrl = "http://tools.2345.com/rili.htm";// 获取农历

	private String day = "";
	private String nongli = "", oldOongli = "";
	private String weather = "", oldWeather = "";
	private String city = null;

	public WeatherTextView2(Context context) {
		super(context);
	}

	public WeatherTextView2(Context context, AttributeSet attrs) {
		super(context, attrs);
		city = Func.getAssetsServer(context, "city");
		Log.i(TAG, "请求天气地址：" + wfUrl + city);
	}

	@Override
	protected void onAttachedToWindow() {
		super.onAttachedToWindow();

		Func.FUNC_TASK.execute(new Runnable() {

			@Override
			public void run() {
				// TODO Auto-generated method stub

				Document doc = null, doc2 = null;
				while (true) {

					try {
						// --------------获取天气情况----------------------
						doc = Jsoup.connect(wfUrl + city).get();
						Elements masthead = doc.select("div.grid");
						Elements lielement = masthead.select("li");
						for (Element element : lielement) {
							if (!element.text().equals("")
									&& !element.select("img").attr("abs:src")
											.equals("")) {

								day = element.select("em").text();
								// 当天天气
								if (day.equals(TimeUtils.getCurrentTimeInString(TimeUtils.D_FORMAT_DATE))) {
									weather = element.select("p").text();
									// Log.i(TAG, "weatherImg:" + weatherImg);
									break;
								}

							}

						}

						if (!oldWeather.equals(weather)) {
							oldWeather = weather;
							mHandler.sendEmptyMessage(0);
						}

						// ---------------获取农历---------------------
						doc2 = Jsoup.connect(nongliUrl).get();
						Elements masthead2 = doc2.select("span.nday");
						if (masthead2.text() != null
								&& !masthead2.text().equals("")) {
							nongli = masthead2.text();
						}

						if (!oldOongli.equals(nongli)) {
							oldOongli = nongli;
							mHandler.sendEmptyMessage(0);
						}

						// Log.i(TAG, "nongli:" + nongli);

						Thread.sleep(30000);
					} catch (InterruptedException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					} catch (IOException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					} catch (ArrayIndexOutOfBoundsException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}

				}

			}
		});

	}

	Handler mHandler = new Handler() {
		@Override
		public void handleMessage(Message msg) {
			// TODO Auto-generated method stub
			super.handleMessage(msg);
			setText(weather.split("[ ]")[0]);
		}
	};

	@Override
	protected void onDetachedFromWindow() {
		super.onDetachedFromWindow();
	}

}
